#include"Boxgame.h"
#include"memory.h"
#include<QString>
#include<QImage>
#include<QRect>
#include<QSoundEffect>//音频类
#include <QMessageBox>
#include "moc_Boxgame.cpp"

BoxGame::BoxGame(){
    initMap();
    pushSoundEffect.setSource(QUrl::fromLocalFile("://map/ji.wav"));
}

void BoxGame::initMap(){
    int graph[10][10] = {
        //初始化地图
        {1,1,1,1,1,1,1,1,1,1},
        {1,3,0,0,0,0,0,0,0,1},
        {1,0,1,0,0,1,1,4,0,1},
        {1,0,0,4,0,5,0,0,1,1},
        {1,3,0,0,0,4,1,0,0,1},
        {1,0,1,0,1,3,1,0,0,1},
        {1,0,0,0,0,0,4,0,0,1},
        {1,0,4,0,1,1,0,1,3,1},
        {1,0,0,0,3,0,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1},
    };
    memcpy(&map[0][0],&graph[0][0],sizeof(int)*100);
}
void BoxGame::drawMap(QPainter* pDraw){
    QString imgURL;
    int i,j;
    for ( i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            switch (map[i][j]) {
            case 0:
                imgURL="://map/road.jpg";
                break;
            case 1:
                imgURL="://map/wall.jpg";
                break;
            case 3:
                imgURL="://map/nets.jpg";
                break;
            case 4:
                imgURL="://map/basketball.jpg";
                break;
            case 5:
            case 8:
                imgURL="://map/cxk.jpg";
                break;
            case 7:
                imgURL="://map/chicken.png";
                break;
            }
            QImage img(imgURL);//贴图
            pDraw->drawImage(QRect(j*img.width(),i*img.height(),img.width(),img.height()),img);//绘制地图
        }

    }
}
void BoxGame::keyDown(QKeyEvent* event){
    //控制人
    int i = getRow(); int j = getCols();
    switch (event->key()) {
    case Qt::Key_W: //上
    case Qt::Key_Up:
        //人走
        if (map[i - 1][j] == 0 || map[i - 1][j] == 3) {
            map[i - 1][j] += 5;
            map[i][j] -= 5;
        }
        //人推箱子走
        if (map[i - 1][j] == 4|| map[i - 1][j]==7) {
            //箱子移动条件
            if (map[i - 2][j] == 0 || map[i - 2][j] == 3) {
                map[i - 2][j] += 4; //箱子来了
                if(map[i-2][j]==7){//如果篮球推进框，放音效
                    pushSoundEffect.play();
                }
                map[i-1][j] += 1; //箱子走了，人来了
                map[i][j] -= 5; //人走了
            }
        }
        break;
    case Qt::Key_S: //下
    case Qt::Key_Down:
        if (map[i + 1][j] == 0 || map[i + 1][j] == 3) {
            map[i + 1][j] += 5;
            map[i][j] -= 5;
        }
        if (map[i + 1][j] == 4 || map[i + 1][j] == 7) {
            if (map[i + 2][j] == 0 || map[i + 2][j] == 3) {
                map[i + 2][j] += 4;
                if(map[i+2][j]==7){
                    pushSoundEffect.play();
                }
                map[i+1][j] += 1;
                map[i][j] -= 5;
            }
        }
        break;
    case Qt::Key_A: //左
    case Qt::Key_Left:
        if (map[i][j - 1] == 0 || map[i][j - 1] == 3) {
            map[i][j - 1] += 5;
            map[i][j] -= 5;
        }
        if (map[i][j - 1] == 4 || map[i][j - 1] == 7) {
            if (map[i][j - 2] == 0 || map[i][j - 2] == 3) {
                map[i][j - 2] += 4;
                if(map[i][j-2]==7){
                    pushSoundEffect.play();
                }
                map[i][j-1] += 1;
                map[i][j] -= 5;
            }
        }
        break;
    case Qt::Key_D: //右
    case Qt::Key_Right:
        if (map[i][j + 1] == 0 || map[i][j + 1] == 3) {
            map[i][j + 1] += 5;
            map[i][j] -= 5;
        }
        if (map[i][j + 1] == 4 || map[i][j + 1] == 7) {
            if (map[i][j + 2] == 0 || map[i][j + 2] == 3) {
                map[i][j + 2] += 4;
                if(map[i][j+2]==7){
                    pushSoundEffect.play();
                }
                map[i][j+1] += 1;
                map[i][j] -= 5;
            }
        }
        break;
    }
}
int BoxGame::getRow(){//获取人行
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (map[i][j] == 5 || map[i][j] == 8) {
                return i;
            }
        }
    }
    return -1;
}
int BoxGame::getCols(){//获取人列
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (map[i][j] == 5 || map[i][j] == 8) {
                return j;
            }
        }
    }
    return -1;
}
bool BoxGame::gameOver(){
    for(int i=0;i<10;i++){
        for (int j = 0; j < 10; j++) {
            if(map[i][j]==4){
                return false;
            }
        }
    }
    emit gameFinished(); // 发出游戏结束信号
    pushSoundEffect.stop(); // 停止音频播放
    return true;
}
